var miao = 59;
var fen = 1;
var sum = 0;
var shibai = 0;
var za;
var zb;
var zc;
var sudu = 0;
var aa = 60;
var bb = 20;
var ta = 1000;
var tb = 250;

function kaishi() {
	miao = 59;
	fen = 2;
	clearInterval(za);
	clearInterval(zb);
	clearInterval(zc);
	za = setInterval("zengjia()", ta);
	zb = setInterval("xialuo()", tb);
	zc = setInterval("shijian()", 1000);

}

function zanting() {
	maio = 0;
	fen = 0;
	var top = document.getElementById("top")
	clearInterval(za);
	clearInterval(zb);
	clearInterval(zc);
	top.childNodes.length = 0;
}

function jieshu() {
	var top = document.getElementById("top");
	var foot = document.getElementById("jifen")
	sum = 0;
	shibai = 0;
	clearInterval(za);
	clearInterval(zb);
	clearInterval(zc);
	top.innerHTML = "";
	foot.children[0].children[0].innerHTML = 0;
	foot.children[0].children[1].innerHTML = "00";
	foot.children[1].children[0].innerHTML = 0;
	foot.children[2].children[0].innerHTML = 0;
	foot.children[3].children[0].innerHTML = 0;

}

function shijian() {
	var jishi = document.getElementById("jifen");
	if(miao > 0 && miao < 10) {
		jishi.children[0].children[1].innerHTML = "0" + miao;
		miao--;
	} else if(miao == 0) {
		if(jishi.children[0].children[0].innerHTML == 0) {
			jishi.children[0].children[1].innerHTML = "0" + miao;
			zanting();
			alert("游戏结束");
			jieshu();
		} else {
			fen--;

			jishi.children[0].children[1].innerHTML = "0" + miao;
			miao = 59;
		}

	} else {
		jishi.children[0].children[1].innerHTML = miao;
		miao--;
	}
	jishi.children[0].children[0].innerHTML = fen;
	sudu = (sum * 60) % ((3 - fen) * 60 + 59 - miao);
	jishi.children[3].children[0].innerHTML = sudu;

}

function zengjia() {
	var top = document.getElementById("top")
	var div = document.createElement("div")
	var zimu = parseInt(Math.random() * 26) + 65
	top.appendChild(div);
	div.style.top = "0px";
	div.style.left = Math.random() * 700 + 10 + "px";
	div.innerHTML = "&#" + zimu + ";";
	div.setAttribute("shuzhi", zimu);
}

function xialuo() {
	var top = document.getElementById("top");
	var jifen = document.getElementById("jifen")

	for(var i = 0; i < top.children.length; i++) {
		var t = parseInt(top.children[i].style.top);
		t += Math.random() * 10 + 5;
		top.children[i].style.top = t + "px";
		if(t >= 370) {
			top.children[i].className = "classa";
			top.children[i].innerHTML = ""
		}
		if(t > 400) {
			top.removeChild(top.children[i]);
			shibai++;
			jifen.children[2].children[0].innerHTML = shibai;
			if(shibai == bb) {
				alert("惜败！！");
				jieshu();
			}
		}
	}
}
window.onkeydown = function() {
	console.log(event.keyCode);
	var zhi = event.keyCode;
	var top = document.getElementById("top")
	var jifen = document.getElementById("jifen")
	for(var i = 0; i < top.children.length; i++) {
		var zimu = top.children[i].getAttribute("shuzhi");
		if(zhi == zimu) {
			top.removeChild(top.children[i]);
			sum++;
			jifen.children[1].children[0].innerHTML = sum;
			if(sum == aa) {
				alert("恭喜过关")
				jieshu();
			}
			break;
		}
	}
}

function shezhi() {

	var shez = document.getElementById("mid");
	if(shez.style.display == "none") {
		shez.style.display = "block"
	} else {
		shez.style.display = "none"
	}
}

function chang(t) {
	var op = document.getElementById("tab");
	var mid = document.getElementById("mid");

	if(t.value == 1) {
		mid.children[0].children[1].innerHTML = 60;
		mid.children[0].children[2].innerHTML = 20;
		ta = 1000;
		tb = 250;
	} else if(t.value == 2) {
		mid.children[0].children[1].innerHTML = 120;
		mid.children[0].children[2].innerHTML = 10;
		ta = 800;
		tb = 180;
	} else {
		mid.children[0].children[1].innerHTML = 180;
		mid.children[0].children[2].innerHTML = 5;
		ta = 500;
		tb = 100;
	}
	aa = mid.children[0].children[1].innerHTML;
	bb = mid.children[0].children[2].innerHTML
}